﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using Db4objects.Db4o;

namespace DHLAppMovil
{
    public class DatabaseContext:IDisposable
    {
        private static DatabaseContext context;
        private static string nombreBD;
        private IObjectContainer database;

        public static void setDBName(string nombre)
        {
            nombreBD = FrmApp.nameDB;
        }

        static DatabaseContext()
        {
            context = new DatabaseContext();
        }

        private DatabaseContext()
        {
            if (FrmApp.nameDB != null && FrmApp.nameDB.CompareTo("") != 0)
            {
                string ruta = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                string fileName = ruta + "\\" + FrmApp.nameDB;
                database = Db4oFactory.OpenFile(fileName);
            }

        }

        public void Dispose()
        {
            database.Close();
        }

        public static DatabaseContext Current
        {
            get { return context; }
        }

        public IObjectContainer Client
        {
            get { return database; }
        }
    }
}
